package com.qdxht.dispenser.ui.activity;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.hjq.bar.TitleBar;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.qdxht.dispenser.R;
import com.qdxht.dispenser.base.BaseActivity;
import com.qdxht.dispenser.databinding.ActivityLocationTestBinding;
import com.qdxht.dispenser.databinding.ActivityMqttTestBinding;

import java.util.List;

public class LocationTestActivity extends BaseActivity<ActivityLocationTestBinding> {

    private TitleBar mTitleBar;
    private TextView mLocationInfoTv;
    private Button mStartLocationBtn;
    private LocationClient mLocationClient;

    @Override
    protected ActivityLocationTestBinding getBinding() {
        return ActivityLocationTestBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        mTitleBar = binding.tbLocationTestTitle;
        mLocationInfoTv = binding.tvLocationInfo;
        mStartLocationBtn = binding.btnStartLocation;
    }

    @Override
    protected void initData() {
        try {
            // 初始化定位
            mLocationClient = new LocationClient(getApplicationContext());
            LocationClientOption option = new LocationClientOption();
            // 设置定位模式为高精度模式
            option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
            // 设置坐标系类型
            option.setCoorType("bd09ll");
            // 设置发起定位请求的间隔时间
            option.setScanSpan(1000);
            // 设置是否需要地址信息
            option.setIsNeedAddress(true);
            // 设置是否需要位置语义化结果
            option.setIsNeedLocationDescribe(true);
            // 设置是否需要返回位置POI信息
            option.setIsNeedLocationPoiList(true);
            // 设置是否打开gps
            option.setOpenGps(true);
            // 设置是否需要返回新鲜度
            option.setLocationNotify(true);
            // 设置是否需要返回海拔高度信息
            option.setIsNeedAltitude(true);
            // 设置定位SDK内部是否使用缓存
            option.setEnableSimulateGps(false);
            
            mLocationClient.setLocOption(option);
            
            mLocationClient.registerLocationListener(new BDAbstractLocationListener() {
                @Override
                public void onReceiveLocation(BDLocation location) {
                    if (location == null) {
                        mLocationInfoTv.setText("定位失败");
                        return;
                    }

                    StringBuilder sb = new StringBuilder();
                    // 添加经纬度信息
                    sb.append("纬度：").append(location.getLatitude()).append("\n");
                    sb.append("经度：").append(location.getLongitude()).append("\n");
                    sb.append("定位精度：").append(location.getRadius()).append("米").append("\n");
                    
                    // 添加地址信息
                    if (location.getLocType() == BDLocation.TypeGpsLocation || 
                        location.getLocType() == BDLocation.TypeNetWorkLocation) {
                        // 添加详细地址信息
                        sb.append("国家：").append(location.getCountry()).append("\n");
                        sb.append("省份：").append(location.getProvince()).append("\n");
                        sb.append("城市：").append(location.getCity()).append("\n");
                        sb.append("区县：").append(location.getDistrict()).append("\n");
                        sb.append("街道：").append(location.getStreet()).append("\n");
                        sb.append("详细地址：").append(location.getAddrStr()).append("\n");
                        // 添加位置描述信息
                        sb.append("位置描述：").append(location.getLocationDescribe()).append("\n");
                    }
                    
                    // 添加定位类型信息
                    sb.append("定位类型：");
                    switch (location.getLocType()) {
                        case BDLocation.TypeGpsLocation:
                            sb.append("GPS定位成功");
                            break;
                        case BDLocation.TypeNetWorkLocation:
                            sb.append("网络定位成功");
                            break;
                        case BDLocation.TypeOffLineLocation:
                            sb.append("离线定位成功");
                            break;
                        case BDLocation.TypeServerError:
                            sb.append("服务端网络定位失败");
                            break;
                        case BDLocation.TypeNetWorkException:
                            sb.append("网络不同导致定位失败，请检查网络是否通畅");
                            break;
                        case BDLocation.TypeCriteriaException:
                            sb.append("无法获取有效定位依据导致定位失败");
                            break;
                        default:
                            sb.append("定位失败");
                            break;
                    }
                    
                    mLocationInfoTv.setText(sb.toString());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtils.show("初始化定位失败：" + e.getMessage());
        }
        mStartLocationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestPermission();
            }
        });
    }

    private void requestPermission() {
        XXPermissions.with(this)
                .permission(Manifest.permission.ACCESS_FINE_LOCATION)
                .permission(Manifest.permission.ACCESS_COARSE_LOCATION)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            startLocation();
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            ToastUtils.show("请在系统设置中授予定位权限");
                            XXPermissions.startPermissionActivity(LocationTestActivity.this, permissions);
                        } else {
                            ToastUtils.show("获取定位权限失败");
                        }
                    }
                });
    }

    private void startLocation() {
        if (mLocationClient != null && !mLocationClient.isStarted()) {
            mLocationClient.start();
            mStartLocationBtn.setText("停止定位");
        } else if (mLocationClient != null && mLocationClient.isStarted()) {
            mLocationClient.stop();
            mStartLocationBtn.setText("开始定位");
            mLocationInfoTv.setText("");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLocationClient != null) {
            mLocationClient.stop();
        }
    }
} 